﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Hosting;
using System.Web.Mvc;
using System.Web.Routing;
using AcademicPlanner.Framework.Libraries.Mvc;
using AcademicPlanner.Framework.Helpers;
using AcademicPlanner.Domain;

namespace AcademicPlanner.Presentation
{
	/// <summary>
	/// AcademicPlannerWebApp is the class the AcademicPlanner application's web front end.
	/// An <code>AcademicPlannerWebApp</code> object is created and launched by the current
	/// <code>HttpHandler</code> (specified in config file). It basically registers the different
	/// web routes when the application is started before continuing execution.
	/// </summary>
	/// <version>1.0</version>
	/// <since>1.0</since>
    public class AcademicPlannerWebApp : System.Web.HttpApplication
    {
		private static AcademicPlannerWebApp current;


		/// <summary>
		/// Gets the current (latest started) <code>AcademicPlannerWebApp</code> object.
		/// </summary>
		/// <version>1.0</version>
		/// <since>1.0</since>
		public static AcademicPlannerWebApp Current { get { return current; } }

		/// <summary>
		/// Gets the instance of the <see cref="T:AcademicPlanner.Domain.DomainMaster">
		/// <code>DomainMaster</code></see> singleton.
		/// </summary>
		/// <seealso cref="T:AcademicPlanner.Domain.DomainMaster">
		/// <version>1.0</version>
		/// <since>1.0</since>
		public DomainMaster DomainMaster { get { return DomainMaster.GetInstance(); } }


        private void RegisterRoutes()
        {
            RouteTable.Routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

			RouteTable.Routes.MapRoute("CompletePersonRegistration", 
					"People/CompleteRegistration/{pFromEmail}/{pToken}/{pPersonId}",
					new { controller = "People", action = "CompleteRegistration", 
							pFromEmail = "", pToken = "", pPersonId = -1 });

			RouteTable.Routes.MapRoute("SetupPerson",
										"People/Setup/{pPersonId}",
										new { controller = "People", action = "Setup", pPersonId = -1 });

            RouteTable.Routes.MapRoute("Site", "{action}", 
										new { controller = "Site", action = "Default" });

            RouteTable.Routes.MapRoute("Default", "{controller}/{action}", 
										new { controller = "Site", action = "Default" });
        }

		/// <summary>
		/// Handles the event to start the application by registering the web routes.
		/// </summary>
		/// <param name="sender">The sender of the event.</param>
		/// <param name="e">The details of the event.</param>
		/// <seealso cref="T:System.Web.Routing.RouteTable"/>
		/// <seealso cref="P:System.Web.Routing.RouteTable.Routes"/>
		/// <seealso cref="M:System.Web.Routing.RouteTable.Routes.MapRoute"/>
		/// <version>1.0</version>
		/// <since>1.0</since>
        protected void Application_Start(object sender, EventArgs e)
        {
			current = this;
            RegisterRoutes();
        }

		//protected void Session_Start(object sender, EventArgs e)
		//{
			
		//}

		//protected void Application_BeginRequest(object sender, EventArgs e)
		//{
			
		//}

		//protected void Application_AuthenticateRequest(object sender, EventArgs e)
		//{

		//}

		//protected void Application_Error(object sender, EventArgs e)
		//{

		//}

		//protected void Session_End(object sender, EventArgs e)
		//{

		//}

		//protected void Application_End(object sender, EventArgs e)
		//{

		//}
    }
}